<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2018/11/29
 * Time: 1:16 PM
 */

namespace app\shop\model;

/**
 * Class Goods
 * @package app\shop\model
 * @OA\Schema(schema="Goods", description="商品模型", type="object",
 *   @OA\Property(property="goods_id", description="商品ID", type="integer", ),
 *   @OA\Property(property="goods_name", description="商品名称", type="string", ),
 *   @OA\Property(property="brand_id", description="品牌ID", type="integer", ),
 *   @OA\Property(property="brand", description="品牌信息", ref="#/components/schemas/Brand", ),
 *   @OA\Property(property="cate_id", description="分类ID", type="integer", ),
 *   @OA\Property(property="category", description="分类信息", ref="#/components/schemas/Category", ),
 *   @OA\Property(property="goods_images", description="商品图片", type="array", @OA\Items( type="string", ), ),
 *   @OA\Property(property="defaultImage", description="商品默认图片", type="string", ),
 *   @OA\Property(property="price", description="商品价格", type="number", ),
 *   @OA\Property(property="stock", description="商品库存", type="integer", ),
 *   @OA\Property(property="goods_desc", description="商品详情", type="string", ),
 *   @OA\Property(property="is_new", description="是否新品", type="boolean", ),
 *   @OA\Property(property="is_hot", description="是否热卖商品", type="boolean", ),
 *   @OA\Property(property="is_recommend", description="是否推荐商品", type="boolean", ),
 *   @OA\Property(property="is_promote", description="是否促销商品", type="boolean", ),
 *   @OA\Property(property="is_discount", description="是否打折商品", type="boolean", ),
 *   @OA\Property(property="skus", description="商品单品", type="array", @OA\Items(ref="#/components/schemas/GoodsSku"), ),
 *   @OA\Property(property="sku_default", description="默认单品ID", type="integer", format="int32", ),
 *   @OA\Property(property="defaultSku", description="默认单品", ref="#/components/schemas/GoodsSku", ),
 *   @OA\Property(property="specs", description="商品所有规格", type="array", @OA\Items(ref="#/components/schemas/GoodsSpec"), ),
 * )
 */
class Goods extends \ShopModel
{
    protected $table = 'sh_goods';

//    protected $type = [
//        'price' => 'float',
//    ];

    use \app\shop\traits\Goods;

    /**
     * 单品
     */
    public function sku()
    {
        return $this->belongsTo('GoodsSku', 'sku_id', 'sku_id');
    }





    /**
     * 商品详情后期处理
     */
    public function infoAfter()
    {
        $count = count($this->skus);
        for ($i = 0; $i < $count; $i++) {
            $currentSku = $this->skus[$i];

            // 商品信息不全 使用默认规格的信息
            if ($this->sku_default === $currentSku->sku_id) {
                $this->defaultSku = $currentSku;
                empty($this->goods_images) && $this->goods_images = $currentSku->sku_images;
                empty($this->price) && $this->price = $currentSku->price;
            }

            // 当前规格没有信息 使用商品信息
            empty($currentSku->sku_name) && $currentSku->sku_name = $this->goods_name;
            empty($currentSku->sku_images) && $currentSku->sku_images = $this->goods_images;
            $currentSku->defaultImage = $currentSku->sku_images[0];
            empty($currentSku->price) && $currentSku->price = $this->price;
        }

        // 第一张图作为默认图片
        $this->defaultImage = $this->goods_images[0];

        $this->doWithSpecs();

        return $this;
    }

    public function doWithSpecs()
    {
        $specsValues = [];
        $specsValuesIds = [];
        $count = count($this->specs);
        for ($i = 0; $i < $count; $i++) {
            $currentSpec = $this->specs[$i];
            // array_column($currentSpec->values, 'value_id')
            $currentSpec->valuesIds = array_map(function($value) {
                return $value->value_id;
            }, $currentSpec->values);
            $specsValues[] = $currentSpec->valuesIds;
            $specsValuesIds = array_merge($specsValuesIds, $currentSpec->valuesIds);
        }
        $this->specsValues = $specsValues;
        $this->specsValuesIds = $specsValuesIds;

        $count = count($this->skus);
        for ($i = 0; $i < $count; $i++) {
            $currentSku = $this->skus[$i];
            $currentSku->specsArray = array_values($currentSku->specs);
        }

        return $this;
    }


}